package com.playball.lottery.frm.set;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.githang.statusbar.StatusBarCompat;
import com.playball.lottery.R;
import com.playball.lottery.ApplicationData;
import com.playball.lottery.base.BaseActivity;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.nt.GetRequestParams;
import com.playball.lottery.util.SysApplication;


public class FeedBackActivity extends BaseActivity {
	private TextView top_title_back;
	private TextView top_title_info;
	private TextView top_title_submit;
	private EditText set_suggestion_EditText01;
	private TextView feedback_text;
	private String suggestion;
	private String userName;
	private ShowTipDialog showdialog = new ShowTipDialog();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.more_feedback);
		StatusBarCompat.setStatusBarColor(this, Color.RED, true);
		SysApplication.getInstance().addActivity(this);
		top_title_back = (TextView) findViewById(R.id.top_title_back);
		top_title_info = (TextView) findViewById(R.id.top_title_info);
		top_title_info.setText("建议反馈");
		top_title_submit = (TextView) findViewById(R.id.top_title_right_btn);
		top_title_submit.setVisibility(View.VISIBLE);
		top_title_submit.setText("提交");
		set_suggestion_EditText01 = (EditText) findViewById(R.id.set_suggestion_EditText01);
		set_suggestion_EditText01.setFilters(new InputFilter[]{new InputFilter.LengthFilter(140)});
		set_suggestion_EditText01.addTextChangedListener(textWatcher);
		feedback_text = (TextView) findViewById(R.id.feedback_text);
		feedback_text.setText(Html.fromHtml("您还可以输入" + "<font color=red>" + 140 + "</font>" + "个字"));
		top_title_submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				suggestion = set_suggestion_EditText01.getText().toString();
				if (suggestion.equals("")) {
					Toast.makeText(FeedBackActivity.this, R.string.can_not_be_empty, Toast.LENGTH_SHORT).show();
				} else {
					AsyncLoader task = new AsyncLoader();
					task.execute("115");// 120
				}
			}
		});
		top_title_back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onKeyDown(KeyEvent.KEYCODE_BACK, null);
			}
		});
	}

	public void onResume() {
		super.onResume();
		
	}

	public void onPause() {
		super.onPause();
		
	}

	private TextWatcher textWatcher = new TextWatcher() {

		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
			if (!set_suggestion_EditText01.getText().toString().equals("")) {
				set_suggestion_EditText01.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
				feedback_text.setText(Html.fromHtml("您还可以输入" + "<font color=red>"
						+ (140 - set_suggestion_EditText01.getText().toString().length()) + "</font>" + "个字"));
			} else {
				feedback_text.setText(Html.fromHtml("您还可以输入" + "<font color=red>" + 140 + "</font>" + "个字"));
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {

		}

	};

	public class AsyncLoader extends AsyncTask<String, Integer, String> {// 继承AsyncTask

		// 1.准备运行
		protected void onPreExecute() {
			showdialog.showDialog(FeedBackActivity.this);
		}

		// 2.正在后台运行
		@Override
		protected String doInBackground(String... params) {
			String strResult;
			try {
				if (!ApplicationData.getInstance().getUserName().equals("")) {
					userName = ApplicationData.getInstance().getUserName();
				} else {
					userName = "";
				}
				JSONObject param = new JSONObject();
				param.put("Name", userName);
				param.put("Suggestion", suggestion);
				param.put("Type", "bug反馈");
				param.put("Contact", " jjjj@qq.com");
				String action = params[0];
				// 获取请求返回值
				strResult = GetRequestParams.getRequestResult(param.toString(), action, getApplicationContext());
				return strResult;
			} catch (Exception e) {
				// TODO: handle exception
				Log.e("error", e.toString());
				return null;
			}
		}

		// 3.进度更新
		protected void onProgressUpdate(Integer... progress) {
		}

		// 4.完成后台任务
		protected void onPostExecute(String result) {
			if (result == null) {
				Toast.makeText(FeedBackActivity.this, getString(R.string.problem_01), Toast.LENGTH_LONG).show();
				return;
			}
			if (result != null) {
				try {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					// 接下来的就是JSON对象的操作了
					if (Integer.parseInt(js.getString("Code")) == 0) {
						// 弹框
						// ShowCustomDialog.Builder alertbBuilder = new
						// ShowCustomDialog.Builder(FeedBackActivity.this);
						// alertbBuilder.setTitle("退出此界面");
						// alertbBuilder.setMessage(js.getString("Data"));
						// alertbBuilder.setNegativeButton("确认", new
						// DialogInterface.OnClickListener() {
						// @Override
						// public void onClick(DialogInterface dialog, int
						// which) {
						onKeyDown(KeyEvent.KEYCODE_BACK, null);
						Toast.makeText(FeedBackActivity.this, "提交反馈成功", Toast.LENGTH_LONG).show();
						// dialog.dismiss();
						// }
						// }).setPositiveButton("取消", new
						// DialogInterface.OnClickListener() {
						// @Override
						// public void onClick(DialogInterface dialog, int
						// which) {
						// dialog.dismiss();
						// }
						// })
						// .createCustomDialog().show();
					}

				} catch (JSONException ex) {
					// 异常处理代码
					Log.e("error", ex.toString());
				} finally {
					showdialog.closeDialog();
				}
			}
			showdialog.closeDialog();
		}

		// 5.完成后操作
		protected void onCancelled() {
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			this.finish();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		SysApplication.getInstance().removeActivity(this);
	}
}
